#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "Mumzik.h"
#include "mumzik_func.h"
#include "qsort.h"

using namespace std;

int main()
{
	size_t const NUM_BASK_EXCH = 5;
	Mumzik* mums_beg = NULL; // pointer to the first element mumzik's array
	Mumzik* mums_end = NULL; // pointer to the element following the last mumzik's array
	size_t common_apples = 0; // number of appeles in common basket
	char* file_in = "share.in";
	char* file_out = "share.out";

	if ( Read_Mumzik_from_file(&mums_beg, &mums_end, file_in) )
	{
		cout<<"Unable to open file \""<<file_in<<"\"";
		cout<<endl<<endl;
		system("pause");
		return 1;
	}

	Sort_all_Mumzik (mums_beg, mums_end, comparator_num_app() );
	common_apples = Global_exchange(mums_beg, mums_end, NUM_BASK_EXCH);
	Sort_all_Mumzik (mums_beg, mums_end, comparator_index_bask() );

	if ( Write_Mumzik_to_file(mums_beg, mums_end, file_out) )
	{
		cout<<"Unable to open file \""<<file_out<<"\"";
		cout<<endl<<endl;
		system("pause");
		return 2;
	}

	ofstream out (file_out, std::ios::app);
	if (!out)
	{
		cout<<"Unable to open file \""<<file_out<<"\"";
		cout<<endl<<endl;
		system("pause");
		return 2;
	}

	out<<endl<<common_apples;

	out.close();
	delete[] mums_beg;
	return 0;
}